﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataAccess;
using System.IO;

namespace QUANLYKHACHSAN_N4
{
    public partial class frmFormMain : DevComponents.DotNetBar.Office2007RibbonForm
    {

        public frmPrint pr = new frmPrint();
        public TaiKhoan_Info tk;
        frmDangNhap fdn;

        #region ==================FORMMAIN
        public frmFormMain()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            MaximumSize = MinimumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            Left = Top = 0;
            //DangXuat();
            //btnDangNhap_Click(null, null);
        }
        private void frmFormMain_LocationChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
                WindowState = FormWindowState.Normal;
            Top = Left = 0;
        }
        private void frmFormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!BatLoi.DgResult("Bạn muốn thoát khỏi chương trình"))
                e.Cancel = true;
        }
        private void ribbonControl1_ExpandedChanged(object sender, EventArgs e)
        {
            if (!ribbonControl1.Expanded)
                ribbonControl1.Expanded = true;
        }

        #endregion

        #region==================SHOW CHUC NANG
        public void ShowPrint()
        {
            if (FormIsInactive("frmPrint"))
                ShowChucNang(pr);
        }
        void CloseAllMDI(Form frm)
        {
            foreach (Form f in this.MdiChildren)
                if (f != frm)
                    f.Close();
        }

        public void ShowChucNang(Form frm)
        {
            if (!this.IsMdiContainer)
                IsMdiContainer = true;
            frm.MdiParent = this;
            frm.LocationChanged += new EventHandler(frm_LocationChanged);
            frm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            frm.BackColor = this.BackColor;
            frm.Size = new Size(this.Width - 15, Height - ribbonControl1.Height - 8);
            frm.WindowState = FormWindowState.Normal;
            frm.Show();
            //CloseAllMDI(frm);
        }
        void frm_LocationChanged(object sender, EventArgs e)
        {
            Form frm = sender as Form;
            frm.Top = frm.Left = 0;
        }
        public bool FormIsInactive(string FormName)
        {
            foreach (Form frm in MdiChildren)
                if (frm.Name == FormName)
                {
                    frm.Show();
                    frm.BringToFront();                    
                    return false;
                }
            return true;
        }
        #endregion

        #region===================LOGIN LOGOUT

        public void GetAccount()
        {
            DangNhap();
            tk = fdn.tk;
            lbThongTin.Text = "Xin chào: " + tk.hovaten + "\nMã Nhân Viên: " + tk.manv + "\nChức vụ: " + tk.chucvu;
        }
        void DangNhap()
        {
            btnDangNhap.Image = global::QUANLYKHACHSAN_N4.Properties.Resources.Lock_Lock_icon;
            btnDangNhap.Tooltip = "Đăng Xuất";
            rbThongTin.Visible = rbDoiMatKhau.Visible = rbThongTinNV.Visible = true;
        }
        void DangXuat()
        {
            tabTaiKhoan.Select();
            btnDangNhap.Image = global::QUANLYKHACHSAN_N4.Properties.Resources.Lock_Unlock_icon;
            btnDangNhap.Tooltip = "Đăng Nhập Hệ Thống";
            rbThongTin.Visible = rbDoiMatKhau.Visible = rbThongTinNV.Visible = false;
            foreach (Form f in this.MdiChildren)
                f.Close();
        }
        #endregion

        #region===================NHOM CHUC NANG

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (tk == null)
            {
                fdn = new frmDangNhap(this);
                fdn.ShowDialog();
            }
            else
            {
                tk = null;
                DangXuat();
            }
        }

        private void btnQlNhanVien_Click(object sender, EventArgs e)
        {

            if (FormIsInactive("frmQuanLyNhanVien"))
            {
                frmQuanLyNhanVien frm = new frmQuanLyNhanVien();
                ShowChucNang(frm);
            }

        }

        private void btnQLThietBi_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyThietBi"))
            {
                frmQuanLyThietBi frm = new frmQuanLyThietBi();
                ShowChucNang(frm);
            }
        }

        private void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau frm = new frmDoiMatKhau(tk);
            if (frm.ShowDialog() == DialogResult.OK)
                tk = frm.tk;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void btnTaiKhoan_Click(object sender, EventArgs e)
        {
            NhanVien_Info nv = NhanVien_Info.GetByID(tk.manv)[0];
            frmThongTinNhanVien f = new frmThongTinNhanVien(nv);
            f.ShowDialog();
        }
        #endregion

        private void btnQLPhong_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyPhong"))
            {
                frmQuanLyPhong frm = new frmQuanLyPhong();
                ShowChucNang(frm);
            }
        }

        private void btnQlTaiKhoan_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyTaiKhoan"))
            {
                frmQuanLyTaiKhoan frm = new frmQuanLyTaiKhoan();
                ShowChucNang(frm);
            }
        }

        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
            SaveFileDialog sv = new SaveFileDialog();
            sv.Filter = "Database File|*.mdf";
            if (sv.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string s = sv.FileName;
                    s = s.Substring(0, s.LastIndexOf('.'));
                    File.Copy(Application.StartupPath + @"\App_Data\DBQLKS.mdf", s + ".mdf");
                    File.Copy(Application.StartupPath + @"\App_Data\DBQLKS_log.ldf", s + "_log.ldf");
                    DgResul.ShowDiag("Sao Lưu Dữ Liệu Thành Công");
                }
                catch (Exception)
                {
                    DgResul.ShowDiag("Sao Lưu Dữ Liệu Thất Bại");

                }
            }
        }

        private void btnPhucHoi_Click(object sender, EventArgs e)
        {
            try
            {
                DgResul.ShowDiag("Khi bạn chọn phục hồi csdl, tất cả dữ liệu hiện tại sẽ mất, bạn nên sao lưu CSDL trước");
                if (DgResul.ShowDiag("Bạn có muốn sao lưu dữ liệu", true))
                    btnSaoLuu_Click(null, null);
                OpenFileDialog op = new OpenFileDialog();
                op.Title = "Chọn File Cơ Sở Dữ Liệu Muốn Phục Hồi";
                op.Filter = "Database File|*.mdf";
                if (op.ShowDialog() == DialogResult.OK)
                    if (DgResul.ShowDiag("Bạn có muốn phục hồi CSDL đã chọn"))
                    {
                        string s = op.FileName;
                        s = s.Substring(0, s.LastIndexOf('.'));
                        File.Delete(Application.StartupPath + @"\App_Data\DBQLKS.mdf");
                        File.Delete(Application.StartupPath + @"\App_Data\DBQLKS_log.ldf");
                        File.Copy(s + ".mdf", Application.StartupPath + @"\App_Data\DBQLKS.mdf");
                        File.Copy(s + "_log.ldf", Application.StartupPath + @"\App_Data\DBQLKS_log.ldf");
                        DgResul.ShowDiag("Phục hồi dữ liệu thành công");
                    }
            }
            catch (Exception)
            {
                DgResul.ShowDiag("Phục hồi dữ liệu thất bại");
            }
        }

        private void btnNhatKy_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmNhatKySaoLuu"))
            {
                frmNhatKySaoLuu frm = new frmNhatKySaoLuu();
                ShowChucNang(frm);
            }
        }

        private void btnThongTin_Click(object sender, EventArgs e)
        {
            frmThongTinPhanMem frm = new frmThongTinPhanMem();
            frm.ShowDialog();
        }

        private void btnQlKhachHang_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyKhachHang"))
            {
                frmQuanLyKhachHang frm = new frmQuanLyKhachHang();
                ShowChucNang(frm);
            }
        }

        private void btnDichVu_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyDichVu"))
            {
                frmQuanLyDichVu frm = new frmQuanLyDichVu();
                ShowChucNang(frm);
            }
        }

        private void btnQLHoaDon_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyHoaDon"))
            {
                frmQuanLyPhieuThu frm = new frmQuanLyPhieuThu();
                ShowChucNang(frm);
            }
        }



        private void btnQLPhieuThu_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyPhieuThu"))
            {
                frmQuanLyPhieuThu frm = new frmQuanLyPhieuThu();
                ShowChucNang(frm);
            }
        }

        private void btnQlPhieuChi_Click(object sender, EventArgs e)
        {
            if (FormIsInactive("frmQuanLyPhieuChi"))
            {
                frmQuanLyPhieuChi frm = new frmQuanLyPhieuChi();
                ShowChucNang(frm);
            }
        }

        private void tabTrangThaiPhong_Click(object sender, EventArgs e)
        {
            tabTaiKhoan.Select();
            if (FormIsInactive("frmTrangThaiPhong"))
            {
                frmTrangThaiPhong frm = new frmTrangThaiPhong();
                ShowChucNang(frm);
            }
        }
    }
}
